package com.dq.domain.sys.role;


import com.dq.context.Constant;
import com.dq.vo.baseVo.VO;
import com.dq.domain.base.BaseEntity;

import com.dq.domain.sys.section.SysSection;
import com.dq.utils.TimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.NotBlank;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.List;

/**
 * 角色
 * 部门角色说明类
 *
 * @author gewenzhen
 */
@Data
@Entity
@Table(uniqueConstraints = {@UniqueConstraint(columnNames="code")})
@ApiModel(value = "角色")
public class SysRole extends BaseEntity implements VO.ToVO {
    @NotBlank(message = "角色名称不能为空")
    @ApiModelProperty(value = "角色名称")
    private String roleName;

    @ApiModelProperty(value = "备注")
    private String remark;

    @ApiModelProperty("编码")
    @NotBlank(message = "编码不能为空")
    private String code;

    @ManyToOne
    @ApiModelProperty(value = "所属部门", hidden = true)
    private SysSection dept;

    @Enumerated(EnumType.ORDINAL)
    @ApiModelProperty(value = "状态")
    private Status status;

    @ApiModelProperty("充值提成比例")
//    @Column(columnDefinition="decimal(19,2) default 0.00")
    protected BigDecimal topRatio;
    @ApiModelProperty("消耗提成比例")
//    @Column(columnDefinition="decimal(19,2) default 0.00")
    protected BigDecimal expendRatio;

    public enum Status {
        off("禁用"), on("启用"),;

        Status(String name) {
            this.name = name;
        }

        public String name;
    }

    public SysRole(){

    }

    public SysRole(String roleName,String code,String remark,Status status, boolean deleted){
        this.roleName = roleName;
        this.code = code;
        this.remark = remark;
        this.status = status;
        this.deleted = deleted;

        this.setVersion(Constant.VERSION_VALUE);
        this.setCreateTime(System.currentTimeMillis());
        this.setLastModifyTime(System.currentTimeMillis());
    }

    @Data
    @ApiModel(value = "用户添加角色视图")
    public static class UserAddRoleVo{
        @ApiModelProperty(value = "角色id")
        private Long key;
        @ApiModelProperty("角色名称")
        private String label;
    }

    public UserAddRoleVo toUserAddRoleVo(){
        SysRole.UserAddRoleVo userAddRoleVo = new SysRole.UserAddRoleVo();
        userAddRoleVo.key = this.id;
        userAddRoleVo.label = this.roleName;
        return userAddRoleVo;
    }

    @Data
    @ApiModel(value = "角色视图")
    public static class Vo implements VO {
        @ApiModelProperty(value = "角色id")
        private Long id;
        @ApiModelProperty(value = "角色名称")
        private String roleName;
        @ApiModelProperty("角色编码")
        private String code;
        @ApiModelProperty(value = "备注")
        private String remark;

        @ApiModelProperty(value = "创建时间")
        private String createTime;
        @ApiModelProperty(value = "更新时间")
        private String updateTime;
        @ApiModelProperty("状态")
        private String statusEn;
        @ApiModelProperty("状态")
        private String statusCn;
        @ApiModelProperty("充值提成比例")
        protected BigDecimal topRatio;
        @ApiModelProperty("消耗提成比例")
        protected BigDecimal expendRatio;

        @ApiModelProperty(value = "创建人id")
        private Long createUserId;
        @ApiModelProperty(value = "所属部门id")
        private Long sysDeptId;
        @ApiModelProperty(value = "所属部门名称")
        private String sysDeptName;

        @ApiModelProperty(value = "所在部门id列表")
        private List<Long> deptIdList;
        @ApiModelProperty(value = "菜单ids")
        private List<Long> menuIdList;
    }

    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id = this.id;
        vo.roleName = this.roleName;
        vo.code = this.code;
        vo.remark = this.remark;
        vo.createTime = TimeUtil.longToString(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.updateTime = TimeUtil.longToString(this.lastModifyTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.statusEn = this.status == null ? null : this.status.toString();
        vo.statusCn = this.status == null ? null : this.status.name;
        vo.topRatio = this.getTopRatio();
        vo.expendRatio = this.getExpendRatio();

        vo.sysDeptId = this.getDept() == null ? null : this.getDept().getId();
        vo.sysDeptName = this.getDept() == null ? "" : this.getDept().getSimpleName();

        return vo;
    }

    @Data
    public static class RoleInit {
        public String roleName;
        public String code;
        public String remark;
        public Boolean deleted;

        public RoleInit() {
        }

        public RoleInit(String code, String roleName, String remark, Boolean deleted) {
            this.roleName = roleName;
            this.code = code;
            this.remark = remark;
            this.deleted = deleted;
        }
    }
}
